// pages/personal_center/leavemanagement/leavemanagement.js
//引入封装请求
import request from "../../../utils/request";
var dateTimePicker = require('../../../utils/dateTimePicker.js');


Page({
  data: {
    //时间滚动选择器
    isPickerRender: false, //开启时间选择器
    isPickerShow: false, //显示时间选择器
    pickerConfig: {
      endDate: true,
      column: "minute", //分
      dateLimit: true,
      initStartTime: "",
      initEndTime: "",
      limitStartTime: "",
      limitEndTime: "",
      isTeaStu: 0//0学生1老师
    },
    //表单滚动选择器
    showTopTips: false, //错误提示

    leaType: [],
    leaTypeIndex: 0,

    isAgree: false
  },
  async onLoad() {
    //发送请求
    let list = await request('/WxInterface/WxSelectLeaTypeForSelect');
    let leaType = [];
    for (var i = 0; i < list.length; i++) {
      leaType.push(list[i].text);
    }

    /* 取出登录信息 字符串*/
    let userInfo = wx.getStorageSync('userInfo');
    //转换
    let UserInfo = JSON.parse(userInfo)
    this.setData({
      leaType: leaType,

      /* 转完之后 存值当前页面  */
      Idnumber: UserInfo[4],
      userInfo: UserInfo,
    })


  },

  bindLeaTypeChange: function (e) {
    console.log('请假类型发生选择改变，携带值为', e.detail.value);

    this.setData({
      leaTypeIndex: e.detail.value
    })
  },


  //时间滚动选择器显示
  pickerShow: function () {
    this.setData({
      isPickerShow: true,
      isPickerRender: true,
      chartHide: true
    });
  },
  //时间滚动选择器隐藏
  pickerHide: function () {
    this.setData({
      isPickerShow: false,
      chartHide: false
    });
  },

  //时间滚动选择器更改之后的时间
  setPickerTime: function (val) {
    // console.log(val);
    let data = val.detail;
    //去掉最后的秒
    // data.startTime = data.startTime.slice(0, -3);
    // data.endTime = data.endTime.slice(0, -3);
    this.setData({
      startTime: data.startTime,
      endTime: data.endTime
    });
  },

  //请假注意事项弹窗
  tapHelp: function (e) {
    if (e.target.id == 'leave') {
      this.hideHelp();
    }
  },
  showHelp: function (e) {
    this.setData({
      'leave_note': true
    });
  },
  hideHelp: function (e) {
    this.setData({
      'leave_note': false
    });
  },

  //请假注意事项是否阅读
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  //重置
  formReset: function (e) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
  },
  //表单提交
  formSubmit: async function (e) {
    let startDate = e.detail.value.startDate; //开始时间
    let endDate = e.detail.value.endDate; //结束时间
    let urgName = e.detail.value.urgName; //紧急联系人姓名
    let urgTelephone = e.detail.value.urgTelephone; //紧急联系人电话
    let leaTypeID = Number(e.detail.value.leaTypeID) + 1; //请假类型,页面选择是从0开始
    let caseStatus = e.detail.value.caseStatus ? 1 : 0; //家长知情状态
    console.log(e.detail.value.caseStatus)
    console.log(caseStatus)
    let leaCause = e.detail.value.leaCause; //请假事由
    let isAgree = e.detail.value.isAgree //是否阅读请假注意事项
    let teacherID = 0,
      studentID = 0; //老师id学生id

    // 获取本地存储
    let leave = wx.getStorageSync('Leave') ? wx.getStorageSync('Leave') : '[]';
    let leaveArr = JSON.parse(leave) //转换为对象模式

    let gradeID = leaveArr.gradeID; //年级id
    let classID = leaveArr.classID; //班级id
    let dormID = leaveArr.dormID; //宿舍id

    //登录类型为0【老师登录】，1【学生登陆】
    if (this.data.userInfo[1] == 0) {
      teacherID = this.data.userInfo[0]
    } else {
      studentID = this.data.userInfo[0];
    }

    if ((startDate == null || startDate == "") && (endDate == null || endDate == "")) {
      wx.showToast({
        title: '请选择请假时间',
        icon: 'none'
      })
      return;
    }


    if (urgName == null || urgName == "") {
      wx.showToast({
        title: '请填写紧急联系人姓名',
        icon: 'none'
      })
      return;
    }
    if (urgTelephone == null || urgTelephone == "") {
      wx.showToast({
        title: '请填写紧急联系人电话',
        icon: 'none'
      })
      return;
    }
    //定义电话号码正则表达式
    let phoneReg = /^0?(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[89])[0-9]{8}$/;
    if (!phoneReg.test(urgTelephone)) {
      wx.showToast({
        title: '紧急联系人电话格式错误',
        icon: 'none'
      })
      return;
    }

    if (leaTypeID <= 0) {
      wx.showToast({
        title: '请选择请假类型',
        icon: 'none'
      })
      return;
    }

    //startDate endDate
    var days = new Date(endDate).getTime() - new Date(startDate).getTime();
    var day = parseInt(days / (1000 * 60 * 60 * 24));
    if (day >= 1 && caseStatus == 0) {
      wx.showToast({
        title: '请假时间超过一天，家长必须知情',
        icon: 'none'
      })
      return;
    }
    if (leaCause == null || leaCause == "") {
      wx.showToast({
        title: '请填写请假原因',
        icon: 'none'
      })
      return;
    }

    if (isAgree[0] != "agree") {
      wx.showToast({
        title: '请阅读请假注意事项',
        icon: 'none'
      })
      return;
    }

    console.log('form发生了submit事件，携带数据为：', e.detail.value);

    let Data = {
      startDate: startDate,
      endDate: endDate,
      urgName,
      urgTelephone,
      leaCause: leaCause,
      leaTypeID: leaTypeID,
      caseStatus: caseStatus,
      studentID: studentID,
      teacherID: teacherID,
      gradeID: gradeID,
      classID: classID,
      dormID: dormID
    }
    console.log('Data：', Data);

    //发送新增请求
    let formData = await request('/WxInterface/InsertLeave', Data);
    console.log(formData);

    if (formData.State) {
      wx.reLaunch({
        url: '/pages/personal_center/stu_leavedata/stu_leavedata'
      })
      wx.showToast({
        title: formData.Text,
        icon: '',
        duration: 1800
      });

    } else {
      wx.showToast({
        title: formData.Text,
        icon: 'none',
        duration: 1800
      })
   }
  }

})